import { ref } from 'vue'
import { adapter, request } from '@/request'
import { auditResultByPost } from '@/core/api/priceSheet'
import type { ApproveStatusEnum } from '@/core/enums/status'
import type { RequestWrapper } from '@/core/interfaces/common'
import type { PriceSheet } from '@/core/interfaces/priceSheet'
import { message, Modal } from 'ant-design-vue'
import type { AxiosError, AxiosResponse } from 'axios'

export interface AuditResult {
  userId: number
  priceAskId: number
  approverId: number
  approverName: string
  approveStatus: ApproveStatusEnum
  approveInfo: string
  specialApproveIds: number[]
}
export interface AuditResultType extends Omit<AuditResult, 'tpType'> {
  tpType?: string
}

export default function updateAuditResult() {
  const service = adapter<RequestWrapper<PriceSheet>>(request)
  const loading = ref(false)
  const response = ref<Partial<PriceSheet>>({})

  const fetch = (data: AuditResultType) => {
    const { url, method } = auditResultByPost()
    loading.value = true

    return service({
      config: {
        url,
        method,
        data
      },
      callback: (res) => {
        // 处理 code 为 -1 的情况
        if (res.data.code === -1) {
          message.error(res.data.msg || '操作失败')
          return
        }
        response.value = res.data.data
      },
      error: (error: Error | AxiosResponse<RequestWrapper<PriceSheet>>) => {
        console.log(error, '285')
        // 处理其他错误情况
        let errorMsg = '操作失败'
        if (error instanceof Error) {
          errorMsg = error.message || errorMsg
        }
        // message.error(errorMsg)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
